package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sc.nft.dao.CollectionAttentionDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.CollectionAttention;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.CollectionAttentionOrCancelDTO;
import com.sc.nft.entity.dto.SecondCollectionConfigDTO;
import com.sc.nft.entity.dto.WhetherAttentionDTO;
import com.sc.nft.entity.vo.WhetherAttentionVO;
import com.sc.nft.service.CollectionAttentionService;
import com.sc.nft.sup.PageRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 藏品关注(CollectionAttention)表服务实现类
 *
 * @author 王伟成
 * @since 2024-03-06 13:55:25
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CollectionAttentionServiceImpl implements CollectionAttentionService {

    private final CollectionAttentionDao collectionAttentionDao;
    @Override
    public void attentionOrCancel(UserInfo user, CollectionAttentionOrCancelDTO collectionAttentionOrCancelDTO) {
        CollectionAttention collectionAttention = collectionAttentionDao.getUserIdAndCollectionId(user.getId(), collectionAttentionOrCancelDTO.getCollectionId());
        // 关注
        if (collectionAttentionOrCancelDTO.getAttention()) {
            attention(user, collectionAttentionOrCancelDTO, collectionAttention);
        }
        // 取消关注
        if (!collectionAttentionOrCancelDTO.getAttention()) {
            cancelAttention(collectionAttention);
        }
    }
    private void cancelAttention(CollectionAttention collectionAttention) {
        if (BeanUtil.isNotEmpty(collectionAttention)) {
            collectionAttention.setStatus(Boolean.FALSE);
            collectionAttention.updateById();
        }
    }
    private void attention(UserInfo user, CollectionAttentionOrCancelDTO collectionAttentionOrCancelDTO, CollectionAttention collectionAttention) {
        // 用户是否已关注
        if (BeanUtil.isEmpty(collectionAttention)) {
            // 没有关注,新增一条.
            attentionSave(user, collectionAttentionOrCancelDTO);
        }
        // 有数据,但是不一定是关注
        if (BeanUtil.isNotEmpty(collectionAttention)) {
            // 未关注,把数据修改为关注
            if (!collectionAttention.getStatus()) {
                collectionAttention.setStatus(Boolean.TRUE);
                collectionAttention.updateById();
            }
        }
    }
    private void attentionSave(UserInfo user, CollectionAttentionOrCancelDTO collectionAttentionOrCancelDTO) {
        CollectionAttention save = new CollectionAttention();
        save.setCollectionId(collectionAttentionOrCancelDTO.getCollectionId());
        save.setUserId(user.getId());
        save.setStatus(Boolean.TRUE);
        save.insert();
    }
    @Override
    public WhetherAttentionVO whetherAttention(UserInfo user, WhetherAttentionDTO whetherAttentionDTO) {
        WhetherAttentionVO whetherAttentionVO=new WhetherAttentionVO();
        CollectionAttention collectionAttention = collectionAttentionDao.getUserIdAndCollectionId(user.getId(), whetherAttentionDTO.getCollectionId());
        if (BeanUtil.isNotEmpty(collectionAttention)) {
            whetherAttentionVO.setAttention(collectionAttention.getStatus());
        }else {
            whetherAttentionVO.setAttention(Boolean.FALSE);
        }
        return whetherAttentionVO;
    }
    @Override
    public IPage<SecondCollectionConfigDTO> attentionList(UserInfo user, PageRequest pageRequest) {
        return collectionAttentionDao.attentionList(user.getId(), pageRequest);
    }
}
